<template lang="">
    <div class="replay">
      <van-cell>
        <template #title>
          <div class="cmt-wrap">
            <van-image round fit="cover" :src="currentItem.aut_photo"></van-image>
            <div class="info">
              <div class="name">{{ currentItem.aut_name }}</div>
              <div class="content">{{ currentItem.content }}</div>
              <div class="time">
                {{ currentItem.pubdate | reltTime}}
              </div>
            </div>
          </div>
        </template>
      </van-cell>
      <van-divider>全部回复</van-divider>
       <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <template #title>
          <div class="cmt-wrap">
            <van-image round fit="cover" :src="item.aut_photo"></van-image>
            <div class="info">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ item.pubdate | reltTime }}
              </div>
            </div>
          </div>
        </template>
      </van-cell>
    </van-list>
    
    <!-- 输入的区域 -->
    <div class="cmt-input-wrap">
      <van-field
        v-model="cmtValue"
        @keyup.enter="send"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>
<script>
import {commentAPI, sendCmtAPI } from "@/api";
export default {
  props:{
      currentItem:{
        required:true,
          type:Object
      }
  },
   data() {
    return {
      list: [],
      loading: false,
      finished: false,
      offset: undefined,
      // 输入的评论内容
      cmtValue: "",
    };
  },
   methods: {
    async onLoad() {
      let res = await commentAPI({
        type: "c",
        source: this.currentItem.com_id,
        offset: this.offset,
      });
      //  console.log(res);
      this.list.push(...res.data.data.results);
      this.offset = res.data.data.last_id;
      this.loading = false;
      if (res.data.data.last_id == res.data.data.end_id) {
        this.finished = true;
      }
    },
    async send() {
      if (this.cmtValue == "") {
        this.$toast("请输入内容");
        return;
      }
      let res = await sendCmtAPI({
        //文章id
        target: this.currentItem.com_id,
        content: this.cmtValue,
        art_id:this.$route.query.id,
      });
      // console.log(res);
      this.list.unshift(res.data.data.new_obj);
      this.currentItem.reply_count++
      this.cmtValue = "";
      this.$toast("回复成功");
    },
  
  },
}
</script>
<style lang="">
    
</style>